package server;

import java.io.*;
import java.net.*;

import maze.*;
import test.Main;

public class Communications {

	ServerSocket server;
	Socket sock;
	
	public Communications() {
		int port = 8888;
		try {
			server = new ServerSocket(port);
			System.out.println("Server is ready for new player");
			this.sock = server.accept();
			System.out.println("New player entered to the server");
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	public void setConnection() throws IOException {
		/*Server s = new Server();
		s.start();*/
		Player pl = Main.game.createNewPlayer();
		Client c = new Client(pl);
		c.start();
	}
}

class Client extends Thread {
	
	Player pl;
	Client(Player pl) {
		this.pl = pl;
	}
	
	public void run() {
		try {
			BufferedReader	netIn = new BufferedReader(new InputStreamReader(
					Main.server.sock.getInputStream()));
			PrintWriter netOut = new PrintWriter(new BufferedWriter(
				new OutputStreamWriter(Main.server.sock.getOutputStream())), true);
			netOut.println("Enter help for list of commands\n");
			netOut.println("Enter your name:\n");
			String fromClient;
			
		while(true){
			fromClient = netIn.readLine();
			String response = "";
			try {
				String[] command = fromClient.split(" ");
				if(fromClient==null) {
					continue;
				}
				if("HELP".equalsIgnoreCase(fromClient)) {
					netOut.println("\nNAME <name> - add player name");
					netOut.println("EXIT - exit from server");
					netOut.println("LOOK - find your location, directions that exists\n");
					continue;
				}
				else if("EXIT".equalsIgnoreCase(fromClient)) {
					break;
				}
				else if(this.pl.name == "") {
					response = "Please enter your name";
				}
				else{
					response = processCommand(fromClient, this.pl);
				}
				if("NAME".equalsIgnoreCase(command[0])) {
					String name = "";
					for(int j = 1; j < command.length; j++){
						name = name + " " + command[j];
					}
					this.pl.name = name;
					response = "Hello, " + name;
				}
				netOut.println(response + "\n");
			} catch(Exception e) {break;}
		}
		Main.server.sock.close();
		Main.server.server.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Player left the server");
		Main.game.endThisPart(pl);
	}
	
	String processCommand(String fromClient, Player pl) {
		String response = "";
		if("LOOK".equalsIgnoreCase(fromClient)) {
			response = "You find yourself in room " + pl.getRoom().getName() +
				". Exists: " + pl.currentRoom.existDoor();
		}
		else response = "Unknown command";
		return response;
	}
}









/*
class Server extends Thread {
	public void run() {
		try {
			BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
			PrintWriter netOut = new PrintWriter(new BufferedWriter(
				new OutputStreamWriter(Main.server.con.sock.getOutputStream())), true);

		while(true){
			String fromMe = in.readLine();
			if(fromMe==null) {
				continue;
			}
			String response = fromMe;
			if("EXIT".equalsIgnoreCase(fromMe)) break;
			netOut.println(response);
		}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				Main.server.con.sock.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
*/
